
/**
 * 授权综合逻辑
 * @param {*} scope 权限代表
 * @param {*} successCallback 成功回调
 * @param {*} failCallback 失败回调
 */
export function pullAuth (scope, successCallback, failCallback) {
  const map = new Map([
    ['scope.userInfo', '用户信息'],
    ['scope.userLocation', '地理位置'],
    ['scope.userLocationBackground', '后台定位'],
    ['scope.address', '通信地址'],
    ['scope.record', '录音功能'],
    ['scope.writePhotosAlbum', '保存到相册'],
    ['scope.camera', '摄像头'],
    ['scope.invoice', '获取发票'],
    ['scope.invoiceTitle', '发票抬头'],
    ['scope.werun', '微信运动步数'],
  ])
  uni.getSetting({
    success (res) {
      if (!res.authSetting[scope]) {
        uni.authorize({
          scope,
          success () { // 允许授权
            successCallback()
          },
          fail () {    // 拒绝授权
            if (map.has(scope)) {
              let word = map.get(scope)
              uni.showModal({
                content: `检测到您没打开${word}权限，是否去设置打开？`,
                confirmText: "确认",
                cancelText: '取消',
                success: (res) => {
                  if (res.confirm) {
                    uni.openSetting({
                      success: (res) => {
                        successCallback()
                      },
                      fail: () => {
                        failCallback()
                      },
                    })
                  } else {
                    failCallback()
                  }
                }
              })
            } else {
              uni.showToast({
                title: '无此授权功能'
              })
              failCallback()
            }
          }
        })
      } else {
        successCallback()
      }
    }
  })
}